package cn.signalpha.modules.shop.controller.admin;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.modules.com.dto.WalletDTO;
import cn.signalpha.modules.com.entity.enums.WalletAmountTypeEnum;
import cn.signalpha.modules.com.entity.enums.WalletSourceEnum;
import cn.signalpha.modules.shop.entity.SellerWallet;
import cn.signalpha.modules.shop.service.SellerService;
import cn.signalpha.modules.shop.service.SellerWalletService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/admin/seller/wallet")
@Api(value = "商户钱包明细", tags = "商户钱包明细")
public class SellerWalletController extends BaseController {

	private final SellerService sellerService;
	private final SellerWalletService sellerWalletService;

    @GetMapping("/page")
    @ApiImplicitParams({
        @ApiImplicitParam(name="type", dataType = "string", value = "类型"),
        @ApiImplicitParam(name="amountType", dataType = "string", value = "金额类型"),
        @ApiImplicitParam(name="remark", dataType = "string", value = "备注"),
        @ApiImplicitParam(name="sourceId", dataType = "string", value = "来源id"),
        @ApiImplicitParam(name="sourceType", dataType = "string", value = "来源类型"),
    })
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<SellerWallet>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
        QueryWrapper<SellerWallet> wrapper = sellerWalletService.selectSellerWalletWrapper(params);
        return R.data(sellerWalletService.page(Condition.getPage(query), wrapper));
    }

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<SellerWallet> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(sellerWalletService.getById(id));
	}

	@PostMapping("/save")
	@ApiOperation(value = "充值", notes = "")
	public R save(@Valid @RequestBody WalletDTO walletDTO) {
		walletDTO.setSourceId(getUser().getUserId().toString());
		walletDTO.setSourceType(WalletSourceEnum.SYSTEM.getType());
		walletDTO.setRemark(WalletSourceEnum.SYSTEM.getName());
		if(walletDTO.getAmountType().equals(WalletAmountTypeEnum.MONEY.getType())){
			return R.status(sellerService.updateSellerMoney(walletDTO));
		}
		return R.fail("请选择正确的金额类型");
	}

}
